package _99练习;

public class SellerRunnable2 implements Runnable {

	int count = 100;
	int s1, s2;
	int time;

	// 方式2：同步方法
	synchronized void sellone() {
		count--;

		System.out.println(Thread.currentThread().getName() + "吃了1个包子，剩下" + count + "个包子");
		if (Thread.currentThread().getName().equals("小明")) {
			s2++;
			time = (int) (Math.random() * 60 + 20);
		} else {
			s1++;
			time = (int) (Math.random() * 40 + 10);
		}
	}

	@Override
	public void run() {
		while (count > 0) {
			sellone();
			try {
				Thread.sleep(time);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println(Thread.currentThread().getName() + "吃完啦");
		System.out.println("小明吃了" + s2 + "只，大明吃了" + s1 + "只");
	}

}
